package com.lemon.smartsafetynew.ui.activity.qrcode;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

/**
 * Created by Andrew on 2017/11/21.
 */

public class QRcodeActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private static final String TAG = QRcodeActivity.class.getSimpleName();

    @Bind(R.id.flash_imageView)
    ImageView flashImageView;
    @Bind(R.id.flash_tv)
    TextView flashTv;
    @Bind(R.id.flash_layout)
    LinearLayout flashLayout;
    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.zbarview)
    ZBarView zbarview;

    private Boolean isFlashOpen = false;
    private String action = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        action = intent.getAction();

        toolbar.setTitle("");
        mainTitle.setText("扫描二维码");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRcodeActivity.this.finish();
            }
        });

        flashLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFlashOpen) {
                    zbarview.closeFlashlight();
                    isFlashOpen = false;
                    flashTv.setText("打开闪光灯");
                    flashImageView.setBackgroundResource(R.mipmap.icon_open_flash);
                } else {
                    zbarview.openFlashlight();
                    isFlashOpen = true;
                    flashTv.setText("关闭闪光灯");
                    flashImageView.setBackgroundResource(R.mipmap.icon_close_flash);
                }

            }
        });

        zbarview.setDelegate(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        zbarview.startCamera();
        zbarview.showScanRect();
        zbarview.startSpot();
    }

    @Override
    protected void onStop() {
        zbarview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zbarview.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
        bundle.putString(CodeUtils.RESULT_STRING, result);
        //add by Andrew.yin 2017/9/13 begin，action是为了判定是否是一键绑定
        if (action != null) {
            bundle.putString("action", action);
        }
        //add by Andrew.yin 2017/9/13 end
        resultIntent.putExtras(bundle);
        QRcodeActivity.this.setResult(RESULT_OK, resultIntent);
        QRcodeActivity.this.finish();

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
        bundle.putString(CodeUtils.RESULT_STRING, "");
        //add by Andrew.yin 2017/9/13 begin，action是为了判定是否是一键绑定
        if (action != null) {
            bundle.putString("action", action);
        }
        //add by Andrew.yin 2017/9/13 end
        resultIntent.putExtras(bundle);
        QRcodeActivity.this.setResult(RESULT_OK, resultIntent);
        QRcodeActivity.this.finish();
    }

}